package com.classic.adapter;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.classic.base.BaseFragment;
import com.classic.duanzhi.TabModel;
import com.classic.fragment.EpisodeFragment;

import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends FragmentPagerAdapter {
    private List<BaseFragment> fragments = new ArrayList<>();
    private List<TabModel> strings = new ArrayList<>();

    public ListAdapter(FragmentManager childFragmentManager) {
        super(childFragmentManager);
        initData(null);
    }

    void initData(List<TabModel> strings) {
        this.strings = strings;
        for (int i = 0; i < strings.size(); i++) {
            fragments.add(getEAFragment(strings.get(i)));
        }
    }

    public ListAdapter(FragmentManager c, List<TabModel> strings) {
        super(c);
        initData(strings);
    }

    private BaseFragment getEAFragment(TabModel i) {
        EpisodeFragment fragment = new EpisodeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(EpisodeFragment.DATA_, i);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public long getItemId(int position) {
        return fragments.get(position).hashCode();
    }

    public void setFragments(List<BaseFragment> fragments) {
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }


    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return strings.get(position).getTitle();
    }
}
